package com.project.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.entity.Asset;
import com.project.entity.AssetFix;
import com.project.entity.AssetType;
import com.project.entity.Staff;
import com.project.service.AssetFixService;
import com.project.service.AssetService;
import com.project.util.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 资产设备维修控制器
 *
 * @author Leo高洋
 * @create 2023-01-20 4:13
 */
@RestController
@RequestMapping("/assetFix")
public class AssetFixController {
    @Autowired
    private AssetService assetService;

    @Autowired
    private AssetFixService assetFixService;

    private final static Logger logger = LoggerFactory.getLogger(AssetFixController.class);

    /**
     * 获取损坏设备的信息
     *
     * @param pageBeans
     * @return
     */
    @PostMapping("/list")
    public R assetFixList(@RequestBody PageBeans pageBeans) {
        logger.info("获取损坏设备的信息");
        Map<String, Object> map = pageBeans.getMap();
        map.put("start", pageBeans.getStart());
        map.put("pageSize", pageBeans.getPageSize());
        List<AssetFix> assetFixList = assetFixService.listAssetDamage(map);
        long total = assetFixService.getAssetDamageTotal(map);
        for (AssetFix assetFix : assetFixList) {
            Asset asset = assetService.getOne(new QueryWrapper<Asset>().eq("id", assetFix.getAssetid()));
            assetFix.setAsset(asset);
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("assetFixList", assetFixList);
        resultMap.put("total", total);
        return R.ok(resultMap);
    }

    /**
     * 添加设备损坏的信息
     *
     * @param assetFix
     * @return
     */
    @PostMapping("/save")
    public R insert(@RequestBody AssetFix assetFix) {
        if (assetFix.getId() == null || assetFix.getId() == -1) {
            logger.info("新增设备维修的信息");
            String date = DateUtil.getCurrentDatePath();
            Asset asset = assetService.getOne(new QueryWrapper<Asset>().eq("id", assetFix.getAssetid()));
            Integer typeId = asset.getTypeId();
            String lsh = "WXLSH-" + date + "-" + typeId + "-" + UUID.randomUUID().toString().substring(0, 2);
            assetFix.setLsh(lsh);
            assetFix.setTbrq(new Date());
            assetFixService.save(assetFix);
            if (assetFix.getIsScrapped() == 2) {
                asset.setSts(3);
                assetService.saveOrUpdate(asset);
            }
            return R.ok("填报成功");
        } else {
            assetFixService.saveOrUpdate(assetFix);
            return R.ok("修改成功");
        }
    }

    /**
     * 根据id显示数据
     *
     * @author Leo高洋
     * @create 2023-03-16 15:50
     */
    @PostMapping("/{id}")
    public R findById(@PathVariable(value = "id") Integer id) {
        logger.info("根据id回显数据");
        AssetFix assetFix = assetFixService.getById(id);
        Asset asset = assetService.getById(assetFix.getAssetid());
        assetFix.setAsset(asset);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("assetFix", assetFix);
        return R.ok(resultMap);
    }

    /**
     * 根据id查询资产设备维修信息集合
     *
     * @author Leo高洋
     * @create 2023-04-05 0:14
     */
    @PostMapping("/listById")
    public R assetFixListById(@RequestBody Integer[] ids) {
        logger.info("根据id查询资产设备维修信息集合");
        List<AssetFix> assetFixList = new ArrayList<>();
        for (Integer id : ids) {
            AssetFix assetFix = assetFixService.getById(id);
            assetFixList.add(assetFix);
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("assetFixList", assetFixList);
        return R.ok(resultMap);
    }
}
